<?php

/**
 * This is the model class for table "{{markups}}".
 *
 * The followings are the available columns in table '{{markups}}':
 * @property integer $id
 * @property integer $uid 
 * @property string $name
 * @property string $edition
 * @property string $manager
 * @property string $types
 * @property string $ntk
 * @property string $date1
 * @property string $editor
 * @property string $namess
 * @property string $date2
 * @property string $conditions
 * @property string $oldconditions
 * @property string $date3
 * @property string $notes
 * @property string $filename 
 * @property string $oldfilename 
 */
class Markups extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Markups the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{markups}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('name, manager, ntk, editor, conditions, oldconditions', 'length', 'max'=>100),
			array('edition, types', 'length', 'max'=>30),
			array('namess, filename, oldfilename', 'length', 'max'=>300),
			array('notes', 'length', 'max'=>1000),
			array('date1, date2, date3', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, uid, name, edition, manager, types, ntk, date1, editor, namess, date2, conditions, oldconditions, date3, notes, filename, oldfilename', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Издание',
			'edition' => '№',
			'manager' => 'Менеджер',
			'types' => 'Тип',
			'ntk' => '№ ТК',
			'date1' => 'Дата',
			'editor' => 'Редактор',
			'namess' => 'Наименование',
			'date2' => 'Дата',
			'conditions' => 'Состояние',
			'date3' => 'Дата',
			'notes' => 'Примечания',
			'filename' => 'Файлы',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('edition',$this->edition,true);
		$criteria->compare('manager',$this->manager,true);
		$criteria->compare('types',$this->types,true);
		$criteria->compare('ntk',$this->ntk,true);
		$criteria->compare('date1',$this->date1,true);
		$criteria->compare('editor',$this->editor,true);
		$criteria->compare('namess',$this->namess,true);
		$criteria->compare('date2',$this->date2,true);
		$criteria->compare('conditions',$this->conditions,true);
		$criteria->compare('date3',$this->date3,true);
		$criteria->compare('notes',$this->notes,true);
		$criteria->compare('filename',$this->filename,true);

		return new CActiveDataProvider(get_class($this), array(
			'pagination'=>array(
				'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
			),
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave()
    {
		if (Yii::app()->user->checkAccess('Markup')) {
			if ($this->isNewRecord) {
				$this->date3 = new CDbExpression('NOW()'); 
				$this->oldconditions = $this->conditions;
				$history = new History;
				$history->username = Yii::app()->user->name;		
				$history->dates = new CDbExpression('NOW()');
				$history->model = get_class($this); 
				$history->conditions = $this->conditions;		
				$history->save();				
			}
			else {	
				if ($this->oldconditions != $this->conditions) {
					$history = new History;
					$history->username = Yii::app()->user->name;		
					$history->dates = new CDbExpression('NOW()');
					$history->model = get_class($this); 
					$history->oldconditions = $this->oldconditions;
					$history->conditions = $this->conditions;	
					$history->save();
					$this->saveAttributes(array('date3' => new CDbExpression('NOW()')));
					$this->saveAttributes(array('oldconditions' => $this->conditions));					
				}
			}			
		}
			
		return parent::beforeSave();
    }	
}